package com.zcoj.controller.User.discussion;

import com.zcoj.annotation.ContestInProgress;
import com.zcoj.core.model.ApiResponse;
import com.zcoj.core.model.PageWrap;
import com.zcoj.pojo.Dto.Discussion;
import com.zcoj.pojo.Vo.CommentReplyVO;
import com.zcoj.pojo.Vo.DiscussionReportVO;
import com.zcoj.pojo.Vo.DiscussionRequestVo;
import com.zcoj.pojo.Vo.SubDiscussionCommentVO;
import com.zcoj.security.dto.LoginUser;
import com.zcoj.service.DiscussionService;
import com.zcoj.util.RedisCaches;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 接口
 * @author 剩xia的盛夏
 * @date 2023/02/24 16:16
 */
@RestController
@RequestMapping("/discussion")
@Api(tags = "接口")
public class DiscussionController {

    @Autowired
    private DiscussionService discussionService;


    @Autowired
    private RedisCaches redisCaches;

    /**
     * 创建
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @PostMapping("/create")
    @ApiOperation("新建")
    public ApiResponse create(@RequestBody Discussion discussion) {
        return discussionService.create(discussion);
    }

    /**
     * 删除
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("根据ID删除")
    public ApiResponse deleteById(@PathVariable Integer id) {
        discussionService.deleteById(id);
        return ApiResponse.success(null);
    }

    /**
     * 根据ID修改
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @PostMapping("/updateById")
    @ApiOperation("根据ID修改")
    public ApiResponse updateById(@RequestBody Discussion discussion) {
        discussionService.updateById(discussion);
        return ApiResponse.success(null);
    }

    /**
     * 分页查询
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @PostMapping("/page")
    @ApiOperation("分页查询")
    public ApiResponse findPage (@RequestBody PageWrap<Discussion> pageWrap) {
        return ApiResponse.success(discussionService.findPage(pageWrap));
    }

    @PostMapping("/pageVo")
    @ApiOperation("分页查询Vo")
    @ContestInProgress
    public ApiResponse findPageVo (@RequestBody PageWrap<Discussion> pageWrap) {
        LoginUser loginUser = redisCaches.getLoginUser();
        if(!(loginUser.getPermission().contains("admin")||loginUser.getPermission().contains("super_admin")||loginUser.getPermission().contains("discussion_admin"))){
            pageWrap.getModel().setStatus(1);
        }
        return ApiResponse.success(discussionService.findPageVo(pageWrap));
    }

    /**
     * 通过ID查询
     * @author 剩xia的盛夏
     * @date 2023/02/24 16:16
     */
    @GetMapping("/finById/{id}")
    @ApiOperation("根据ID查询")
    @ContestInProgress
    public ApiResponse finById(@PathVariable Integer id) {
        return ApiResponse.success(discussionService.findById(id));
    }

    @ApiOperation("")
    @GetMapping("/findByIdVo/{id}")
    @ContestInProgress
    public ApiResponse findByIdVo(@PathVariable Integer id){
        return ApiResponse.success(discussionService.findByIdVo(id));
    }

    @GetMapping("/clickLike/{id}")
    public ApiResponse clickLike(@PathVariable Integer id){
        if(discussionService.clickLike(id)){
            return ApiResponse.success("");
        }else {
            return ApiResponse.failed("点赞失败");
        }
    }

    @PostMapping("/report")
    public ApiResponse reportDiscussion(@RequestBody DiscussionReportVO discussionReportVO){
        if(discussionService.reportDiscussion(discussionReportVO)){
            return ApiResponse.success("提交举报信息成功",null);
        }else {
            return ApiResponse.failed("举报信息还未处理！！！无法继续提交！");
        }
    }


    @PostMapping("/commentAndReplies")
    @ContestInProgress
    public ApiResponse getDiscussionCommentAndReplies(@RequestBody DiscussionRequestVo discussionRequestVo){
        return ApiResponse.success(discussionService.getDiscussionCommentAndReplies(discussionRequestVo));
    }


    @PostMapping("/submitDiscussionComment")
    @ContestInProgress
    public ApiResponse submitDiscussionComment(@RequestBody SubDiscussionCommentVO subDiscussionCommentVO){
        if (discussionService.submitDiscussionComment(subDiscussionCommentVO)){
            return ApiResponse.success("评论成功",null);
        }else {
            return ApiResponse.failed("评论失败");
        }
    }

    @PostMapping("/submitCommentReply")
    @ContestInProgress
    public ApiResponse submitCommentReply(@RequestBody CommentReplyVO commentReplyVO){
        if (discussionService.submitCommentReply(commentReplyVO)){
            return ApiResponse.success(null);
        }else {
            return ApiResponse.failed("评论失败");
        }
    }


}
